class UserMailer < ActionMailer::Base
  
  def send_weather_forecast(setting, options = {  })
    smtp_setting
    recipients setting.user.email
    from @gmail_user_name
    subject "麦吐司网-【#{setting.area.short_name}】#{Time.now.month}月#{Time.now.day}日及未来5天的天气情况"
    sent_on Time.now
    content_type 'text/html'
    body  :setting => setting
    UserSmsEmailsDailyStat.today_stat_by_user_id(setting.user.id).add_emails_count!(options) 
  end

  def send_site_health(setting, options = {  })
    smtp_setting
    recipients setting.user.email
    from @gmail_user_name
    subject "麦吐司网-【#{setting.name}】今日#{setting.todays_stat.health_degree}%可用"
    sent_on Time.now
    content_type 'text/html'
    body  :setting => setting
    UserSmsEmailsDailyStat.today_stat_by_user_id(setting.user.id).add_emails_count!(options)  
  end

  def send_site_health_of_unavailable(unavailable, options = {  })
    smtp_setting
    recipients unavailable.user.email
    from @gmail_user_name
    subject "麦吐司网-【#{unavailable.site_health_setting.name}】#{I18n.l unavailable.created_at, :locale => :'zh-CN', :format => :short}无法访问，今日#{unavailable.site_health_setting.todays_stat.health_degree}%可用"
    sent_on Time.now
    content_type 'text/html'
    body  :unavailable => unavailable
    UserSmsEmailsDailyStat.today_stat_by_user_id(unavailable.user.id).add_emails_count!(options) 
  end

  def send_site_health_of_available(available, options = {  })
    smtp_setting
    recipients available.user.email
    from @gmail_user_name
    subject "麦吐司网-【#{available.site_health_setting.name}】#{I18n.l available.created_at, :locale => :'zh-CN', :format => :short}已经恢复访问，今日#{available.site_health_setting.todays_stat.health_degree}%可用"
    sent_on Time.now
    content_type 'text/html'
    body  :available => available
    UserSmsEmailsDailyStat.today_stat_by_user_id(available.user.id).add_emails_count!(options) 
  end

  def send_password(user, options = {  })
    smtp_setting
    recipients user.email
    from @gmail_user_name
    subject "麦吐司网-您丢失的密码已经找回!"
    sent_on Time.now
    body  :user => user
    UserSmsEmailsDailyStat.today_stat_by_user_id(user.id).add_emails_count!(options) 
  end
  
  def send_info(info,user, options = {  })
    smtp_setting
    recipients user.email
    from @gmail_user_name
    subject "麦吐司网-#{info.email_title}"
    sent_on Time.now
    content_type 'text/html'
    body  :user => user,:info => info
    UserSmsEmailsDailyStat.today_stat_by_user_id(user.id).add_emails_count!(options) 
  end
  
  def fetion_need_verify(user, options = {  })
    smtp_setting
    recipients user.email
    from @gmail_user_name
    subject "麦吐司网-近期没收到短信？移动对旧的飞信进行了限制，麦吐司飞信接口升级啦!"
    sent_on Time.now
    content_type 'text/html'
    body  :user => user
    UserSmsEmailsDailyStat.today_stat_by_user_id(user.id).add_emails_count!(options) 
  end
  
  def capture_hot_post_error(options = {})
    smtp_setting
    recipients 'amon.lei@gmail.com'
    from @gmail_user_name
    subject "麦吐司网-无法抓取到天涯帖子『#{options[:title]}』!"
    sent_on Time.now
    content_type 'text/html'
    body  options
  end
  
  def tian_ya_hot_post_daily_report(options ={})
    smtp_setting
    recipients options[:user].email
    from @gmail_user_name
    subject "麦吐司网-您所追的天涯热帖-《#{options[:hot_post].title}》#{ options[:setting].hot_post_updated? ? '更新啦' : '还没更新' }!"
    sent_on Time.now
    content_type 'text/html'
    body  options
  end
  
  def error_report(options = {})
    smtp_setting
    recipients 'amon.lei@gmail.com'
    from @gmail_user_name
    subject "麦吐司网异常报告：『#{options[:title]}』!"
    sent_on Time.now
    content_type 'text/html'
    body  options
  end
  
  private
  def smtp_setting    
    if Rails.env.production?
      @gmail_user_name = "robot.no.#{[1,2,3,4,5,6,7,8][(rand(1000)%8)]}@mytusi.com"
      ActionMailer::Base.smtp_settings = {
        :enable_starttls_auto => true,
        :address => 'smtp.gmail.com',
        :port => 587,
        :domain => "www.mytusi.com",
        :authentication => :plain,
        #:user_name => "robot.no.5@mytusi.com",
        :user_name => @gmail_user_name,
        :password => '123'
      }
    else
      @gmail_user_name = "test1.mytusi.com"
      ActionMailer::Base.smtp_settings = {
        :enable_starttls_auto => true,
        :address => 'smtp.gmail.com',
        :port => 587,
        :domain => "www.mytusi.com",
        :authentication => :plain,
        :user_name => @gmail_user_name,
        :password => '!@##@!!!@@##$$'
      }
    end
  end
end
